﻿// CWndINC.cpp: 实现文件
//

#include "pch.h"
#include "GameHacker.h"
#include "CWndINC.h"
#include "afxdialogex.h"


// CWndINC 对话框

IMPLEMENT_DYNAMIC(CWndINC, CDialogEx)


CWndINC::CWndINC(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PAGE_1, pParent)
	, m_ExeNormal(_T(""))
	, m_ExeInjected(_T(""))
{

}

CWndINC::~CWndINC()
{
}

void CWndINC::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT4, m_ExeNormal);
	DDX_Text(pDX, IDC_EDIT6, m_ExeInjected);
	DDX_Control(pDX, IDC_TAB1, m_Tab);
}

void CWndINC::RefreshTab()
{
	int n = m_Tab.GetCurSel();
	for (int i = 0; i < m_nCurPage; i++) {
		m_arrPages[i]->ShowWindow(i == n);
	}
}


BEGIN_MESSAGE_MAP(CWndINC, CDialogEx)
	ON_BN_CLICKED(IDC_OPEN_PROC, &CWndINC::OnBnClickedOpenProc)
	ON_BN_CLICKED(IDC_OPEN_PROC2, &CWndINC::OnBnClickedOpenProc2)
	ON_WM_SIZE()
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CWndINC::OnTcnSelchangeTab1)
	ON_BN_CLICKED(IDC_OPEN_PROC3, &CWndINC::OnBnClickedAnalysis)
END_MESSAGE_MAP()


// CWndINC 消息处理程序


bool CWndINC::InstallPage(CDialogEx* wnd, UINT id, CString&& _name, bool show)
{
	if (m_nCurPage >= MAX_PAGE) return false;
	m_arrPages[m_nCurPage] = wnd;
	m_arrPages[m_nCurPage]->Create(id, &m_Tab);
	m_arrPages[m_nCurPage]->ShowWindow(show);

	//设置tab控件上的页面名称和顺序
	m_Tab.InsertItem(m_nCurPage, _name);

	//调整tab页面显示位置
	CRect rect;
	m_Tab.GetClientRect(&rect);
	rect.bottom -= 30;
	m_arrPages[m_nCurPage]->MoveWindow(&rect);

	m_nCurPage++;
	return true;
}


void CWndINC::OnBnClickedOpenProc()
{
	if (wndProcess.DoModal() == IDOK) {
		m_WndModLst[0].GetModList(wndProcess.m_dwPid);
		m_ExeNormal = wndProcess.m_Exe;
		UpdateData(false);
	}
}


void CWndINC::OnBnClickedOpenProc2()
{
	if (wndProcess.DoModal() == IDOK) {
		m_WndModLst[1].GetModList(wndProcess.m_dwPid);
		m_ExeInjected = wndProcess.m_Exe;
		UpdateData(false);
	}
}


BOOL CWndINC::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	InstallPage(&m_WndModLst[0], IDD_MOD_LIST, L"正常进程", TRUE);
	InstallPage(&m_WndModLst[1], IDD_MOD_LIST, L"感染进程", TRUE);
	InstallPage(&m_ModAnalysis, IDD_MOD_ANALYSIS, L"分析对比", TRUE);
	RefreshTab();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CWndINC::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	CDialogEx::OnSize(nType, cx, cy);
	if (nType == 1 || m_Tab.m_hWnd == 0) return;
	//最大化和调整窗口尺寸时,相应的设置tab页面的大小和位置
	CRect rect;
	m_Tab.GetClientRect(&rect);
	rect.bottom -= 30;
	for (int i = 0; i < m_nCurPage && m_arrPages[i]; i++)
		m_arrPages[i]->MoveWindow(&rect);
}


void CWndINC::OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	RefreshTab();
	*pResult = 0;
}


void CWndINC::OnBnClickedAnalysis()
{
	// TODO: 在此添加控件通知处理程序代码
	int nCountNormal = m_WndModLst[0].m_lstModule.GetItemCount();
	int nCountInjected = m_WndModLst[1].m_lstModule.GetItemCount();
	if (!nCountNormal || !nCountInjected) return;
	m_ModAnalysis.m_lstModAnalysis.DeleteAllItems();
	for (int i = 0; i < nCountNormal; i++) {
		CString txt = m_WndModLst[0].m_lstModule.GetItemText(i, 3); // 比对文件位置
		int j = 0;
		for (; (j < nCountInjected) && (m_WndModLst[1].m_lstModule.GetItemText(j, 3).MakeUpper() != txt.MakeUpper()); j++);
		if (j == nCountInjected) {
			m_ModAnalysis.m_lstModAnalysis.InsertItem(0, m_WndModLst[0].m_lstModule.GetItemText(i, 0));
			m_ModAnalysis.m_lstModAnalysis.SetItemText(0, 1, m_WndModLst[0].m_lstModule.GetItemText(i, 1));
			m_ModAnalysis.m_lstModAnalysis.SetItemText(0, 2, m_WndModLst[0].m_lstModule.GetItemText(i, 2));
			m_ModAnalysis.m_lstModAnalysis.SetItemText(0, 3, m_WndModLst[0].m_lstModule.GetItemText(i, 3));
			m_ModAnalysis.m_lstModAnalysis.SetItemText(0, 4, L"减少");
			m_ModAnalysis.m_lstModAnalysis.SetItemData(0, 1);
		}
	}
	for (int i = 0; i < nCountInjected; i++) {
		CString txt = m_WndModLst[1].m_lstModule.GetItemText(i, 3); // 比对文件位置
		int j = 0;
		for (; (j < nCountNormal) && (m_WndModLst[0].m_lstModule.GetItemText(j, 3).MakeUpper() != txt.MakeUpper()); j++);
		if (j == nCountNormal) {
			m_ModAnalysis.m_lstModAnalysis.InsertItem(0, m_WndModLst[1].m_lstModule.GetItemText(i, 0));
			m_ModAnalysis.m_lstModAnalysis.SetItemText(0, 1, m_WndModLst[1].m_lstModule.GetItemText(i, 1));
			m_ModAnalysis.m_lstModAnalysis.SetItemText(0, 2, m_WndModLst[1].m_lstModule.GetItemText(i, 2));
			m_ModAnalysis.m_lstModAnalysis.SetItemText(0, 3, m_WndModLst[1].m_lstModule.GetItemText(i, 3));
			m_ModAnalysis.m_lstModAnalysis.SetItemText(0, 4, L"增加");
			m_ModAnalysis.m_lstModAnalysis.SetItemData(0, 0);
		}
	}
}
